﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAL;

namespace BlogWebSite.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        public ActionResult Login()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Login(User logininformation)
        {
            Blog blog;
            var correctLogin = DB
                .AuthenticateLogin(logininformation.Email, logininformation.Password, out blog);

            if (correctLogin)
            {
                var user = DB.GetUser(logininformation.Email);

                Session["Login"] =
                    new UserCredentials()
                    {
                        Email = user.Email,
                        Name = user.Name,
                        Country = user.Country,
                        BlogName = blog.BlogName,
                        ID = user.UserId
                    };
                return RedirectToAction("Index", "Blog", new { id = blog.BlogName });
            }
            else
                return RedirectToAction("Login");
        }

        public ActionResult LogOut()
        {
            Session["Login"] = null;
            return RedirectToAction("Index", "Home");
        }
    }
}